package securityproject;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import securityproject.utils.Constants;
import securityproject.utils.LogUtils;
import securityproject.utils.RuleUtils;

public class PresentRule extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8164290679416541463L;

	public final static String RULE_NAME = "rule";

	public final static String NEW_RULE = "newRule";

	public final static String NEW_RULE_VALUE = "yes";

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String newRule = req.getParameter(NEW_RULE);
		String tmpRule = req.getParameter(RULE_NAME);

		init(resp, newRule, tmpRule);
	}

	public void init(HttpServletResponse resp, String newRule, String tmpRule)
			throws TransformerFactoryConfigurationError, IOException {
		String rule = "";
		String xmlRule;

		if ((newRule != null)
				&& newRule.equals(NEW_RULE_VALUE)) {

			xmlRule = Constants.XML_NEW_RULE;

		} else {

			rule = tmpRule;
			xmlRule = RuleUtils.getXmlRule(rule);
		}

		try {

			File xmlRuleFile = new File(xmlRule);
			if (!xmlRuleFile.exists()) {
				RuleUtils.createCustomXMLRule(rule);
			}

			// Instantiate a DocumentBuilderFactory.
			DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dfactory.newDocumentBuilder();

			TransformerFactory tFactory2 = TransformerFactory.newInstance();

			Document inDoc = null;
			try {
				inDoc = dBuilder.parse(xmlRule);

				addLogFiles(inDoc);

			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// activating suffix rules
			Transformer transformer3 = tFactory2
					.newTransformer(new StreamSource(
							Constants.XML_RULES_PRESENTER));

			transformer3.transform(new DOMSource(inDoc), new StreamResult(resp
					.getOutputStream()));

			// transformer3.transform(new DOMSource(inDoc),
			// new StreamResult(new FileOutputStream(
			// Constants.XML_RULES_FOLDER + "bla.xml")));

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			// resp.getWriter().write(blaMessage);
		}
	}

	private void addLogFiles(Document inDoc) {
		Element root = inDoc.getDocumentElement();

		Element files = inDoc.createElement("files");

		File logFolder = new File(Constants.LOG_FOLDER);
		for (File logFile : logFolder.listFiles()) {
			Element file = inDoc.createElement("file");
			Text text = inDoc.createTextNode(LogUtils.getLogName(logFile
					.getName()));
			file.appendChild(text);

			files.appendChild(file);
		}

		root.appendChild(files);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
